
function generateTree(){
    //准备生成属性结构的数据
    $.ajax({
        url:"/menu/getWholeTree",
        type:"post",
        dataType:"json",
        success:function (response) {
            var result = response.result;
            if (result=="SUCCESS"){
                //创建一个json对象用于存储ztree所做的设置
                var setting = {
                    "view":{
                        "addDiyDom":myAddDiyDom,
                        "addHoverDom":myAddHoverDom,
                        "removeHoverDom":myRemoveHoverDom
                    },
                    "data":{
                        "key":{
                            "url":"fu"
                        }
                    }
                };
                //获取数据
                var zNodes = response.data;
                //初始化树形结构
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);

            }if (result=="FAILED"){
                console.log("操作失败"+response.message);
            }
        },
        error:function (response) {
            console.log(response.status+" "+response.statusText);
        }
    });
}


//在鼠标移入结点范围时添加按钮组
function myAddHoverDom(treeId,treeNode) {

    var btnGroupId = treeNode.tId + "_btnGrp";
    if ($("#"+btnGroupId).length > 0){
        return;
    }
    //准备各个按钮的HTML标签
    var addBtn = "<a id='"+treeNode.id+"' class='addBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='添加节点'><i class='fa fa-fw fa-plus rbg '></i></a>";
    var removeBtn = "<a id='"+treeNode.id+"' class='removeBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='删除结点'><i class='fa fa-fw fa-times rbg '></i></a>";
    var editBtn = "<a id='"+treeNode.id+"' class='editBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='修改结点'><i class='fa fa-fw fa-edit rbg '></i></a>";

    //获取节点级别
    var level = treeNode.level;
    var btnHtml = "";
    //判断当前结点级别
    if (level == 0){
        btnHtml = addBtn;
    }
    if (level == 1){
        btnHtml = addBtn +" "+ editBtn;
        //判断是否有子节点
        var length = treeNode.children.length;
        if (length == 0){
            btnHtml = btnHtml + " " + removeBtn;
        }
    }
    if (level==2){
        btnHtml = editBtn + " " + removeBtn;
    }

    //找到附着按钮的超链接
    var anchorId = treeNode.tId + "_a";
    //在超链接后面附加span元素操作
    $("#"+anchorId).after("<span id='"+btnGroupId+"'+>"+btnHtml+"</span>");

}

//在鼠标离开结点范围时删除按钮组
function myRemoveHoverDom(treeId,treeNode) {
    var btnGroupId = treeNode.tId + "_btnGrp";
    $("#"+btnGroupId).remove();
}



function myAddDiyDom(treeId,treeNode) {
    var spanId = treeNode.tId + "_ico";
    $("#"+spanId).removeClass().addClass(treeNode.icon);

}